//
//  AppDelegate.m
//  Cook
//
//  Created by 0.0 on 15-4-13.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "UMSocial.h"
#import "ZWIntroductionViewController.h"
#import "Reachability.h"

@interface AppDelegate ()
{
    NSTimer *_filedTimer;
    UIAlertView *_filedView;
    NSInteger _startNum;
    
    // 网络改变的时候用到的变量
    NSTimer *_noNetTimer;
    UIAlertView *_noNetAlertView;
    NSTimer *_wifiNetTimer;
    UIAlertView *_wifiNetAlertView;
    NSTimer *_wanNetTimer;
    UIAlertView *_wanNetAlertView;
    
    
    
}
@property (nonatomic, strong) ZWIntroductionViewController *introductionView;

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_introductionView release];
    
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    
    //使用通知中心监听kReachabilityChangedNotification通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    //获取访问指定站点的Reachability对象
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    // 获得当前网络的状态
    _startNum = [reach currentReachabilityStatus];
    //让Reachability对象开启被监听状态
    [reach startNotifier];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if (![user objectForKey:@"yes"]) {
        [user setObject:@"success" forKey:@"yes"];
        [self createWelcome];
    }
    else {
        RootViewController *root = [[RootViewController alloc] init];
        [self.window setRootViewController:root];
        [root release];
        if (_startNum == 0) {
            [_filedView show];
            _filedTimer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(filedView) userInfo:nil repeats:YES];
        }
    }
    

    // 设置友盟分享的AppKey
    [UMSocialData setAppKey:@"552f7daafd98c59a88000aae"];
    
   
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNum" object:nil];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNum" object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNum" object:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNum" object:nil];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNum" object:nil];
}

#pragma mark - 网络状态改变时候触发
- (void)reachabilityChanged:(NSNotification*) note
{
    //通过通知对象获取被监听的Reachability对象
    Reachability *curReach = [note object];
    //获取Reachability对象的网络状态
    NetworkStatus status = [curReach currentReachabilityStatus];
    if (status == NotReachable) {
        _noNetAlertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"你已脱离地球网络" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        _noNetTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(noNetAction) userInfo:nil repeats:YES];
        [_noNetAlertView show];
    }
    else if (status == ReachableViaWiFi) {
       _wifiNetAlertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"当前网络为Wifi" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        _wifiNetTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(wifiNetAction) userInfo:nil repeats:YES];
        [_wifiNetAlertView show];
    }
    else if (status == ReachableViaWWAN) {
       _wanNetAlertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"当前网络为2G/3G/4G" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        _wanNetTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(wanNetAction) userInfo:nil repeats:YES];
        [_wanNetAlertView show];
    }
}

#pragma mark - 网络状态改变触发方法
// 无网触发
- (void)noNetAction
{
    [_noNetTimer invalidate];
    [_noNetAlertView dismissWithClickedButtonIndex:0 animated:YES];
    [_noNetAlertView release];
}

// wifi触发
- (void)wifiNetAction
{
    [_wifiNetTimer invalidate];
    [_wifiNetAlertView dismissWithClickedButtonIndex:0 animated:YES];
    [_wifiNetAlertView release];
}

// wan网触发
- (void)wanNetAction
{
    [_wanNetTimer invalidate];
    [_wanNetAlertView dismissWithClickedButtonIndex:0 animated:YES];
    [_wanNetAlertView release];
}


#pragma mark - 创建欢迎界面
- (void)createWelcome
{
    NSArray *coverImageNames = @[@"1", @"2", @"3"];
    NSArray *backgroundImageNames = @[@"m1", @"m2", @"m3"];
    self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
    
    _filedView = [[UIAlertView alloc] initWithTitle:@"数据请求失败,请检查网络" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [self.window addSubview:self.introductionView.view];
    [_introductionView release];
    
    
    __block AppDelegate *weakSelf = self;
    __block NSInteger sNum = _startNum;
    
    self.introductionView.didSelectedEnter = ^() {
        [weakSelf.introductionView.view removeFromSuperview];
        weakSelf.introductionView = nil;
        RootViewController *root = [[[RootViewController alloc] init] autorelease];
        [weakSelf.window setRootViewController:root];
        if (sNum == 0) {
            [_filedView show];
            _filedTimer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(filedView) userInfo:nil repeats:YES];
        }
    };

}

// 开启应用无法时候触发
- (void)filedView
{
    [_filedTimer invalidate];
    [_filedView dismissWithClickedButtonIndex:0 animated:YES];
    [_filedView release];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

@end
